package com.bankcomm.location;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.bankcomm.BankcommApplication;
import com.bankcomm.R;

public class BaiduLocation {
	/** 上下文对象 */
	private Context context = null;

	/** 定位Handler */
	private Handler mHandler = null;

	/** 定位定时器 */
	public Timer timer = null;
	public MyLocationListenner myListener = new MyLocationListenner();

	private LocationClient mLocClient;

	 
    private static BaiduLocation instance = null;
    
    public String currentLat="";
    public String currentLon="";
     
    
    public static BaiduLocation getInstance() {
        if( instance == null ) {
            instance = new BaiduLocation();
        }
        return instance;
    }   
	
	public BaiduLocation() {
		mLocClient = BankcommApplication.getInstance().mLocationClient;
		mLocClient.registerLocationListener(myListener);
	}
	
	public void getLocation(Activity bv, Handler handler){
		context = bv;
		mHandler = handler;

		Message m = new Message();
		m.what = 1001;
		mHandler.sendMessage(m);
		setLocationOption();

		// 5秒之后定位失败, 则使用默认经纬度
		timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				unbindLocationProviders();

				Message mes = new Message();
				mes.what = 3001;
				mes.obj = context.getString(R.string.location_failure_default);
				mHandler.sendMessage(mes);
			}
		}, 5000);
		bindLocationProvider();
	}
	

	private void bindLocationProvider() {
		
		mLocClient.start();

	}

	public void unbindLocationProviders() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (mLocClient != null) {
			mLocClient.stop();
		}
	}


	protected void sendLatLonMessage(BDLocation location) {
		Message message = new Message();
		message.what = 2001;

		Bundle bundle = new Bundle();
		bundle.putDouble("lat", location.getLatitude());
		bundle.putDouble("lon", location.getLongitude());

		if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
			// 省：location.getProvince()
			// 市：location.getCity()
			// 区/县：location.getDistrict()
			// ddr location.getAddrStr()
			String city= location.getCity();
			if(city.contains("市")){
				city=city.replace("市", "");
			}
			bundle.putString("city",city);
		}

		message.setData(bundle);
		mHandler.sendMessage(message);
	}

	// 设置相关参数
	private void setLocationOption() {
		LocationClientOption option = new LocationClientOption();
		option.setServiceName("com.bankcomm.new");
		// option.setScanSpan(3000);
		// option.setCoorType("bd09ll");
		option.disableCache(true);
		option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先
		option.setPoiNumber(10);
		option.setAddrType("all");
		option.disableCache(true);
		mLocClient.setLocOption(option);
	}

	public class MyLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			unbindLocationProviders();
			if (mHandler != null) {
				Message m = new Message();
				m.what = 1001;
				mHandler.sendMessage(m);
			}
			if (location == null)
				return;
			if (mHandler != null) {
				sendLatLonMessage(location);
				mHandler = null;
			}else{
				setLatLonValue(location);
			}
		}

		private void setLatLonValue(BDLocation location) {
			currentLat=String.valueOf(location.getLatitude());
		    currentLon=String.valueOf(location.getLongitude());
		}

		public void onReceivePoi(BDLocation poiLocation) {
			unbindLocationProviders();
			if (poiLocation == null) {
				return;
			}
		}
	}
}
